<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
    <title>添加商品规格</title>
    <script th:src="@{/static/assets/js/jquery.min.js}"></script>
    <script src="http://libs.useso.com/js/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
    <link rel="stylesheet" th:href="@{/static/component/pear/css/pear.css}" />

    <!--    日期插件css-->
    <link rel="stylesheet" type="text/css" th:href="@{/static/pear-admin/css/bootstrap.min.css}" />
    <link rel="stylesheet" th:href="@{/static/pear-admin/css/dcalendar.picker.css}"/>

    <style>
        .layui-form-label{
            width: 110px;
        }
    </style>

</head>
<body>
<form class="layui-form" th:action="@{/pear/add/spec}" method="post" id="fm" autocomplete="off">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">规格名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入规格名称" class="layui-input">
                    </div>

                    <label class="layui-form-label">商品编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="p_id" lay-verify="title" autocomplete="off" placeholder="请输入商品编号" class="layui-input">
                    </div>
                    <label class="layui-form-label">组序</label>
                    <div class="layui-input-block">
                        <input type="text" name="order" lay-verify="title" autocomplete="off" placeholder="请输入组序" class="layui-input">
                    </div>
                    <label class="layui-form-label">规格标题</label>
                    <div class="layui-input-block">
                        <input  type="text" name="title" lay-verify="title" autocomplete="off" placeholder="规格标题" class="layui-input">
                    </div>
                    <label class="layui-form-label">规格价格</label>
                    <div class="layui-input-block">
                        <input type="text" name="price" lay-verify="title" autocomplete="off" placeholder="规格价格" class="layui-input">
                    </div>
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <input type="text" name="description" lay-verify="title" autocomplete="off" placeholder="规格描述" class="layui-input">
                    </div>

                </div>


            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                添加
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script th:src="@{/static/component/layui/layui.js}"></script>
<script th:src="@{/static/component/pear/pear.js}"></script>

<script>


    layui.use(['form','jquery'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let jq = $.noConflict();
        // 获取<meta>标签中封装的_csrf信息 ,否则会请求403
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var headers = {"X-CSRF-TOKEN": token}

        form.on('submit(user-save)', function (data) {
            layer.confirm('您确定要添加该商品规格吗?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);

                let loading = layer.load();
                $.ajax({
                    url:'/pear/add/spec',
                    data: jq('#fm').serialize(),
                    dataType:'json',
                    type:'post',
                    headers: headers,
                    success: function (result) {
                        layer.close(loading);
                        if(result.success){
                            layer.msg(result.msg,{icon:1,time:1000},function(){
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        }else{
                            layer.msg(result.msg,{icon:2,time:1000});
                        }
                    },
                    error:function () {
                        layer.close(loading);
                        layer.msg("添加商品规格失败",{icon:2,time:1000});
                    }
                })
            })
            return false;
        });



    })
</script>
<script>
</script>
</body>
</html>